import { GroupDoubleAttacker } from "../class/group/terminal/combat/double_attacker";
import { GroupAttacker } from "../class/group/terminal/combat/offender";
import { GroupQuadAttacker } from "../class/group/terminal/combat/quad_attacker";
import { errLog } from "../util/log";

const supportedTypes=[
    GroupDoubleAttacker,
    GroupQuadAttacker,
    GroupAttacker,
];
export const globalWarfare={

    /**
     * 
     * @returns {GroupAttacker[]}
     */
    getGroups:function(){
        let groups=Game.attackers;
        if(groups!==undefined && groups instanceof Array){
            return groups;
        }

        this.restoreMemory();

        return Game.attackers;
    },
    run:function(){
        let groups = this.getGroups();

        for(let flagName in Game.flags){
            let flag = Game.flags[flagName];

            let targetRoomName = flag.pos.roomName;
            let [oper, groupType, spawnRoomName, ...args] = flag.name.split(':');
            if(oper!=='attack'){
                continue;
            }

            flag.remove();
            if(!Game.groups[spawnRoomName]){
                continue;
            }
            for(let GroupType of supportedTypes){
                if(GroupType.name === groupType){
                    try{
                        groups.push(new GroupType(Game.rooms[spawnRoomName], targetRoomName, ...args));
                    }catch(e){
                        errLog(e);
                    }
                    break;
                }
            }
        }


        for(let i=0; i<groups.length; i++){
            const group = groups[i];

            if(!group.spawning && group.size === 0){
                groups.splice(i, 1);
                i--;
                continue;
            }

            try{
                group.spawn();
            }catch(e){
                errLog(e);
            }
            try{
                group.run();
            }catch(e){
                errLog(e);
            }
        }
        return OK;
    },
    saveMemory(){
        let objGroups = Game.attackers;
        let memGroups = [];
        
        if(!(objGroups instanceof Array)){
            Memory.attackers = [];
            return OK;
        }
        for(let objGroup of objGroups){
            // let objGroup = objGroups[mainRoomName];
            let memGroup;
            try{
                memGroup = {
                    class: objGroup.getClassName(),
                    obj: objGroup.serialize(),
                };
            }catch(e){
                errLog(e);
            }

            memGroups.push(memGroup);
        }
        Memory.attackers=memGroups;
        return OK;
    },
    restoreMemory(){
        let newGroups=[];
        let memGroups = Memory.attackers;
        if(!(memGroups instanceof Array)){
            Memory.attackers = memGroups = [];
        }
        for(let mem of memGroups){
            if(!mem){
                continue;
            }
            for(let GroupType of supportedTypes){
                if(GroupType.name === mem.class){
                    try{
                        let objGroup = new GroupType().deserialize(mem.obj);
                        if(objGroup !== undefined){
                            newGroups.push(objGroup);
                        }
                    }catch(e){
                        errLog(e);
                    }
                    break;
                }
            }
        }
        Game.attackers=newGroups;
        return OK;
    },
}